package com.hd.manager.config;

import com.hd.appcommon.entity.Privilege;
import com.hd.appcommon.service.IPrivilegeService;
import com.hd.manager.interceptor.JwtTokenInterceptor;
import com.hd.manager.interceptor.PrivilegeInterceptor;
import com.hd.manager.shiro.token.PrivilegeCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;
//第二步：注册拦截器
//继承 WebMvcConfigurationSupport ，重写 addInterceptors 方法
@Configuration
public class WebInterceptorConfig extends WebMvcConfigurationSupport {

    @Autowired
    private IPrivilegeService privilegeService;

    @Bean
    public PrivilegeInterceptor privilegeInterceptor() {
        //自定义权限拦截器
        PrivilegeInterceptor privilegeInterceptor = new PrivilegeInterceptor();
        //初始化自定义权限缓存
        List<Privilege> privileges = privilegeService.listAllAble();
        PrivilegeCache.init(privileges);
        privilegeInterceptor.addPrivilegePath(privileges);
        return privilegeInterceptor;
    }

    @PostConstruct
    public void initPrivilegeCache() {

    }

    @Bean
    public JwtTokenInterceptor getJwtInterceptor() {
        return new JwtTokenInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getJwtInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(Arrays.asList("/login/submit", "/logout", "/upload/picture", "/phLock/push", "/testIdc/**","/getCode", "/login/tokenNum","/dd/**","/greencode/list","/greencode/save","/dept/deptList",
                        "/dd/task/deptscopes","/dd/calback","/Database/login","/contestCredits/login","/miniProgram/login","/training_plan/import","/TrainingPlanDetailManage/import_detail","/courseScore/miniProgram/**","/user/*","/JieShi/*","/Room/*"));
    }
}
